package aufgabe3_chat;

import java.util.LinkedList;
import java.util.List;


public class Server_Administration extends Thread{

	//Sleeptime
	private final int SLEEPTIME = 1000;
	
	private static Server_Administration_Control control;
	private static Logger logger;
	
	private static List<Server> server_list = new LinkedList<Server>();
	
	public Server_Administration() {
		logger = new Logger();
		control = new Server_Administration_Control(100, "root",logger);
		logger.log("Klasse", "Was", "Nachricht");
		logger.log("Server Administration", "Server Administration", "Erzeugt");
	}
	
	public void run(){
		logger.log("Server Administration", "Server", "Erzeuge");
		Server new_server = new Server(50000, control,logger);
		
		logger.log("Server Administration","Server","Hinzu Fuegen ");
		Server_Administration.server_list.add(new_server);
		
		logger.log("Server Administration","Server","Starten"); 
		new_server.start();
		
		logger.log("Server Administration","Server"," Lauft");
		
		logger.log("Server Administration", "Server Administration", "Run: " + control.server_run());
		
		while(control.server_run()){
			my_sleep(SLEEPTIME);
		}
		
		logger.log("Server Administration", "Server Administration", "Beende Server");
		while( ! Server_Administration.server_list.isEmpty() ){
			Server_Administration.server_list.get(0).stop_runing();
			Server_Administration.server_list.remove(0);
		}
		
		logger.log("Server Administration", "Server Administration", "Warte auf Clients");
		while(control.server_threads_running()){
			my_sleep(SLEEPTIME);
		}
		logger.log("Server Administration", "Server Administration", "Beende");
	}
	
	private void my_sleep(int time){
		try {
			sleep(time);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
